#!/usr/bin/perl

#require "./user_admin.pl"; #user_admin sub functions
require Tk::LabEntry;
require Tk::DialogBox;
use Tk;

my $window = MainWindow->new;
$window->geometry("500x500");
$window->title("Alarm Command Terminal");
my $menu = $window->Menu();
$window->configure(-menu => $menu);
my $User = $menu->Menubutton(-text => "User Commands", -underline=> 0, -tearoff=>0);
$User->command(-label => "Change my Password", -command => \&ChangeMyPass);
$User->command(-label => "Add a User", -command => \&AddUser);
$User->command(-label => "Delete a User", -command => \&DelUser);
$User->command(-label => "Rename a User", -command => \&Rename);
$User->command(-label => "Change a User's Password", -command => \&ChangePass);
$User->command(-label => "Disable a User", -command => \&Disable);
$User->command(-label => "Enable a User", -command => \&Enable);
$User->command(-label => "Set a User's Type", -command => \&SetType);
$User->command(-label => "Show List of Users", -command => \&ListUser);
my $Sensor = $menu->Menubutton(-text => "Sensor Commands", -underline=> 0, -tearoff=>0);
$Sensor->command(-label => "Add a Sensor", -command => \&AddSensor);
$Sensor->command(-label => "Remove a Sensor", -command => \&but);
$Sensor->command(-label => "List Sensors", -command => \&ListSensor);
my $Health = $menu->Menubutton(-text => "System Commands", -underline=> 1, -tearoff=>0);
$Health->command(-label => "Arm System", -command => \&ArmSystem);
$Health->command(-label => "Disarm System", -command => \&DisarmSystem);
$Health->command(-label => "System Health", -command => \&SystemHealth);
my $Quit = $menu->command(-label => "Quit", -command => \&fin);
$window->Label(-text => "Welcome to the Command Terminal")->pack(-anchor => "n");
$window->Label(-textvariable => \$answer, -justify => "left")->pack(-anchor => "w");
$window->gridColumnconfigure(0, -minsize => 300);
$db = $window->DialogBox(-title => 'Login', -buttons => ['Ok', 'Cancel'], -default_button => 'Ok');
$db->add('LabEntry', -textvariable => \$cuser, -width =>20, -label => 'Username', -labelPack => [-side => 'left'])->pack;
$db->add('LabEntry', -textvariable => \$cpass, -width =>20, -label => 'Password', -show=> '*', -labelPack => [-side => 'left'])->pack;
$result = $db->Show();
if ($result eq "Ok") {
$usertype = system("perl user_admin.pl auth $cuser $cpass") >> 8;
if($usertype == 8){
$usertype = "Administrator";
}elsif($usertype == 9){
$usertype = "Control";
$menu->delete(0,1);
my $ChangeP = $menu->command(-label => "Change Password", -command => \&ChangeMyPass);
}elsif($usertype == 10){
$usertype = "Monitor";
$menu->delete(0,2);
my $ChangeP = $menu->command(-label => "Change Password", -command => \&ChangeMyPass);
}else{
#Either bad password, user is disabled, or no such user.  Error message will be printed by user_admin.pl system call.
fin();
}
$answer = "Welcome $cuser - $usertype\n" . $answer;
}
else{
fin();
}

MainLoop;

print "Run steps to cleanup\n";
exit 0;

################################

sub AddSensor{
my $subwin1 = $window->Toplevel;
$subwin1->geometry("300x150");
$subwin1->title("Add a Sensor");
$subwin1->Label(-text => "Name")->pack();
$subwin1->Entry(-textvariable => \$name)->pack();
foreach (qw/Window Door Motion/){
	$subwin1->Radiobutton(-text => $_, -variable => \$sensor, -value=> $_)->pack(-anchor => 'w');
}
$subwin1->Button(-text=> "OK", -command => sub{AddSensorWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();
}

sub AddSensorWork{
$answer = "Added a ". $sensor . " Sensor called " . $name . "\n";
push @sensorlist, {Name => $name, Type => $sensor};
$name = "";
}

sub ChangeMyPass{
	my $subwin1 = $window->Toplevel;
	$subwin1->geometry("250x250");
	$subwin1->title("Change My Password");
	$subwin1->Label(-text => "Old Password")->pack();
	$subwin1->Entry(-textvariable => \$oldpass, -show=>'*')->pack();
	$subwin1->Label(-text => "New Password")->pack();
	$subwin1->Entry(-textvariable => \$pass, -show=>'*')->pack();
	$subwin1->Label(-text => "Confirm Password")->pack();
	$subwin1->Entry(-textvariable => \$pass2, -show=>'*')->pack();
	$subwin1->Button(-text=> "OK", -command => sub{ChangeMyPassWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();
}

sub ChangeMyPassWork{
	$userwork = "chgmypass $oldpass $pass $pass2\n";
	SetCommands();
}

sub AddUser{
	my $subwin1 = $window->Toplevel;
	$subwin1->geometry("250x250");
	$subwin1->title("Add a User");
	$subwin1->Label(-text => "Name")->pack();
	$subwin1->Entry(-textvariable => \$name)->pack();
	$subwin1->Label(-text => "Password")->pack();
	$subwin1->Entry(-textvariable => \$pass, -show=>'*')->pack();
	$subwin1->Label(-text => "Confirm Password")->pack();
	$subwin1->Entry(-textvariable => \$pass2, -show=>'*')->pack();
	foreach (qw/Admin Control Monitor/){
		$subwin1->Radiobutton(-text => $_, -variable => \$user, -value=> $_)->pack(-anchor => 'w');
	}
	$subwin1->Button(-text=> "OK", -command => sub{AddUserWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();
}

sub AddUserWork{
	$type = substr($user, 0, 1);
	$userwork = "adduser $name $pass $pass2 $type\n";
	SetCommands();
}

sub DelUser{
	my $subwin1 = $window->Toplevel;
	$subwin1->geometry("250x250");
	$subwin1->title("Delete User");
	$subwin1->Label(-text => "Name")->pack();
	$subwin1->Entry(-textvariable => \$name)->pack();
	$confirm = 'N';
	foreach (qw/Confirm?/){
		$subwin1->Radiobutton(-text => $_, -variable => \$confirm, -value=> $_)->pack(-anchor => 'w');
	}
	$subwin1->Button(-text=> "OK", -command => sub{DelUserWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();
}

sub DelUserWork{
	if ($confirm ne 'N'){
		$confirm = 'Y';
	}
	open (CFILE, '>commands.txt');
	print CFILE "deluser $name\n";
	print CFILE "$confirm\n";
	print CFILE "exit";
	close (CFILE);
	UserAdminWork();
}

sub Rename{
	my $subwin1 = $window->Toplevel;
	$subwin1->geometry("250x250");
	$subwin1->title("Rename a User");
	$subwin1->Label(-text => "Current Name")->pack();
	$subwin1->Entry(-textvariable => \$name)->pack();
	$subwin1->Label(-text => "New Name")->pack();
	$subwin1->Entry(-textvariable => \$newname)->pack();
	$subwin1->Button(-text=> "OK", -command => sub{RenameWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();
}

sub RenameWork{
	$userwork = "rename $name $newname\n";
	SetCommands();
}

sub ChangePass{
	my $subwin1 = $window->Toplevel;
	$subwin1->geometry("250x250");
	$subwin1->title("Change a User's Password");
	$subwin1->Label(-text => "Name")->pack();
	$subwin1->Entry(-textvariable => \$name)->pack();
	$subwin1->Label(-text => "New Password")->pack();
	$subwin1->Entry(-textvariable => \$pass, -show=>'*')->pack();
	$subwin1->Label(-text => "Confirm Password")->pack();
	$subwin1->Entry(-textvariable => \$pass2, -show=>'*')->pack();
	$subwin1->Button(-text=> "OK", -command => sub{ChangePassWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();
}

sub ChangePassWork{
	$userwork = "chgpass $name $pass $pass2\n";
	SetCommands();
}

sub Disable{
	my $subwin1 = $window->Toplevel;
	$subwin1->geometry("250x250");
	$subwin1->title("Disable a User");
	$subwin1->Label(-text => "Name")->pack();
	$subwin1->Entry(-textvariable => \$name)->pack(); 
	$subwin1->Button(-text=> "OK", -command => sub{DisableWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();
}

sub DisableWork{
	$userwork = "disuser $name\n";
	SetCommands();
}

sub Enable{
	my $subwin1 = $window->Toplevel;
	$subwin1->geometry("250x250");
	$subwin1->title("Enable a User");
	$subwin1->Label(-text => "Name")->pack();
	$subwin1->Entry(-textvariable => \$name)->pack();
	$subwin1->Button(-text=> "OK", -command => sub{EnableWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();
}

sub EnableWork{
	$userwork = "enuser $name\n";
	SetCommands();
}

sub SetType{
	my $subwin1 = $window->Toplevel;
	$subwin1->geometry("250x250");
	$subwin1->title("Set a User's Type");
	$subwin1->Label(-text => "Name")->pack();
	$subwin1->Entry(-textvariable => \$name)->pack();
	foreach (qw/Admin Control Monitor/){
		$subwin1->Radiobutton(-text => $_, -variable => \$user, -value=> $_)->pack(-anchor => 'w');
	}
	$subwin1->Button(-text=> "OK", -command => sub{SetTypeWork(); $subwin1->destroy if Tk::Exists($subwin1);})->pack();	
}

sub SetTypeWork{
	$type = substr($user, 0, 1);
	$userwork = "settype $name $type\n";
	SetCommands();
}

sub ListUser{
	$userwork = "showusers\n";
	SetCommands();
}


sub SetCommands(){
	open (CFILE, '>commands.txt');
	print CFILE $userwork;
	print CFILE "exit";
	close (CFILE);
	UserAdminWork();	
}

sub UserAdminWork{
	my $useraction = -1;
	$useraction = system("perl user_admin.pl clogin $cuser $cpass") >> 8;
	if ($useraction != 7) {
		open RFILE, "results.txt" or die $!; 
		my @results = <RFILE>;
		$answer = "";
		for $i(0 .. $#results){
			$answer = $answer . "$results[$i]";
		}
	}
	else {
		$answer = "Password has changed.  Please logout and log back in.";
	}
	$name = "";
	$pass = "";
	$pass2 = "";
	$oldpass = "";
	$newname = "";
	$confirm = "";
	unlink "commands.txt";
	unlink "results.txt";
}

sub but{
$answer = "Placeholder\n";
}

sub ListSensor{
$answer = "Sensor List\n";
for $i(0 .. $#sensorlist){
	$answer = $answer . "$i: ";
	for $role (keys %{$sensorlist[$i]}){
		$answer = $answer . " $role = $sensorlist[$i]{$role}\t";
	}
	$answer = $answer . "\n"
}
}

sub ArmSystem{
$answer = "***********************************\n\n\nSYSTEM ARMED\n\n\n***********************************";
}


sub DisarmSystem{
$answer = "***********************************\n\n\nSYSTEM DISARMED\n\n\n***********************************";
}

sub SystemHealth{
$answer = "Sensor Health\n";
$health = "Good";
for $i(0 .. $#sensorlist){
	$answer = $answer . "$i: ";
	for $role (keys %{$sensorlist[$i]}){
		$answer = $answer . " $role = $sensorlist[$i]{$role}\t";
	}
	$answer = $answer . "\tHealth: $health\n"
}
}

sub fin{
$answer = "Goodbye\n";
$window->destroy;

}

sub BEGIN{
$answer = "Status\n";
$sensor= "";
@sensorlist = ({Name => "Front Door", Type => "Door"});
$user= "";
$pass= "";
}
